//
//  DrawingPage.m
//  DemoConnectionBetweenShapes
//
//  Created by duc nguyen van on 8/22/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "DrawingPage.h"
#import "ShapeLine.h"

@implementation DrawingPage
@synthesize viewOne,viewTwo;

#pragma mark -
#pragma mark process draw another line

-(void)resetPoint{
	Bpoint = CGPointMake(-1,-1);
	Epoint = CGPointMake(-1,-1);
}

-(void)createNewLine{
	//[self checkBPointAndEPoint];
	
	float width_view = sqrt(pow((Epoint.x - Bpoint.x),2) + pow((Epoint.y - Bpoint.y),2));
	float rotationValue = atan((Epoint.y - Bpoint.y) / (Epoint.x - Bpoint.x));
	
	ShapeLine *lineView = [[ShapeLine alloc] initWithFrame:CGRectMake(Bpoint.x, Bpoint.y - 10, width_view,20)];
	lineView.backgroundColor = [UIColor clearColor];
	lineView.layer.anchorPoint = CGPointMake(0.0,0.5);
	
	CGPoint temp = lineView.center;
	temp.x -= lineView.frame.size.width / 2; 
	lineView.center = temp;
	
	if (Epoint.x < Bpoint.y) {
		rotationValue -= M_PI;
	}
	
	lineView.transform = CGAffineTransformMakeRotation(rotationValue);
	
	[self addSubview:lineView];
	
	lineView.beginViewPoint = selectedBeginPoint;
	lineView.endViewPoint = selectedEndPoint;
	
	[selectedBeginView.BeginLine addObject:lineView];
	[selectedEndView.EndLine addObject:lineView];
	
	//reset and release
	[lineView release];
	[self resetPoint];
}

#pragma mark -
#pragma mark delegateShape

-(void)moveThisShapeWithTouch:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	selectedView = touch.view;
}

#pragma mark -
#pragma mark delegateConnectionRizeView

-(void)panOnThisView:(UIPanGestureRecognizer *)pan andVexter:(CGPoint)vex{
	//CGPoint cen = Rview.center;
//	cen.x += vex.x;
//	cen.y += vex.y;
//	Rview.center = cen;
}

-(void)moveWithPan:(UIPanGestureRecognizer *)pan andID:(int)intID{

}

-(void)tapOnView:(UITapGestureRecognizer *)tap{
	selectedView = tap.view;
}

-(void)sendingTapOnSubView:(UITapGestureRecognizer *)tap andSize:(CGSize)size andSubView:(UIView*)SView{	
	if(Bpoint.x == -1){
		selectedBeginView = (Shape*)SView.superview.superview;
		selectedBeginPoint = SView;
		
		tagSeleted = SView.superview.tag;
		
		Bpoint.x = SView.superview.superview.frame.origin.x + size.width;
		Bpoint.y = SView.superview.superview.frame.origin.y + size.height;
	}
	else if (Bpoint.x != -1) {
		
		if (tagSeleted == SView.superview.tag) {
			[self resetPoint];
			
			Bpoint.x = SView.superview.superview.frame.origin.x + size.width;
			Bpoint.y = SView.superview.superview.frame.origin.y + size.height;
			return;
		}
		
		selectedEndView = (Shape*)SView.superview.superview;
		selectedEndPoint = SView;
		
		Epoint.x = SView.superview.superview.frame.origin.x + size.width;
		Epoint.y = SView.superview.superview.frame.origin.y + size.height;
		[self createNewLine];
	}
	
	NSLog(@"BPoint x: %f and y: %f", Bpoint.x, Bpoint.y);
	NSLog(@"EPoint x: %f and y: %f", Epoint.x, Epoint.y);
}

#pragma mark init

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		arr = [[NSMutableArray alloc] init];
		
		viewOne = [[Shape alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
		viewTwo = [[Shape alloc] initWithFrame:CGRectMake(400, 400, 100, 100)];
		viewThree = [[Shape alloc] initWithFrame:CGRectMake(300, 600, 100, 100)];
		
		viewOne.delegate = self;
		viewOne.cnResizeView.tag = 1;
		viewTwo.delegate = self;
		viewTwo.cnResizeView.tag = 2;
		viewThree.delegate = self;
		viewThree.cnResizeView.tag = 3;
				
		[self addSubview:viewOne];
		[self addSubview:viewTwo];
		[self addSubview:viewThree];
		
		[self resetPoint];
		tagSeleted = 0;
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.//
	//CGContextRef context = UIGraphicsGetCurrentContext();
//	CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
//	CGContextSetLineWidth(context, 3);
//	CGContextMoveToPoint(context, 315, 315);
//	CGContextAddLineToPoint(context, 395, 395);
//	CGContextStrokePath(context);
//	CGContextFillPath(context);
}


- (void)dealloc {
    [super dealloc];
}


@end
